//
//  DraftViewController.m
//  FMSManager
//
//  Created by hongbao.cui on 14-12-9.
//  Copyright (c) 2014年 hongbao.cui. All rights reserved.
//

#import "DraftViewController.h"
#import "MyView.h"
#import "FMNavgationItem.h"
#import "ApplicationUtils.h"
#import "KZDefaultColorViewController.h"
#import "KZColorPicker.h"
#import "ColorPickerView.h"
@interface DraftViewController ()<ColorPickerViewDelegate>{
    UIImageView *bgView;
}
@property(nonatomic,strong)MyView  *drawView;
@property(nonatomic,strong)UIColor  *selectedColor;
@end

@implementation DraftViewController
-(void)back{
    CGRect rect = [UIScreen mainScreen].bounds;
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, rect.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}
- (void)ColorPickerView:(ColorPickerView *)pickerView didChangeColor:(UIColor *)color{
    [self.drawView setLineColor:color];
    [self.drawView setNeedsDisplay];
}
-(void)setBtnClicked:(id)sender{
    CGRect rect = [UIScreen mainScreen].bounds;
    ColorPickerView *picker = [[ColorPickerView alloc] initWithFrame:CGRectMake(0, rect.size.height, rect.size.width, rect.size.height)];
    picker.delegate = self;
    picker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    picker.selectedColor = [UIColor redColor];
    [self.superview addSubview:picker];
    [picker pushToView];
}
-(void)initNavBar{
    CGRect rect = [UIScreen mainScreen].bounds;
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 64)];
    [headerView setImage:[[ApplicationUtils getLocalImage:@"Navigation-Bar_backgroud_44@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
    [headerView setUserInteractionEnabled:YES];
    [self addSubview:headerView];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((rect.size.width-100)/2, 20, 100, 44)];
    titleLabel.text = @"草稿纸";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLabel];
    
    //设置按钮
//    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [setBtn setFrame:CGRectMake(FMSCREENWIDTH-33.0, 20+10.5, 23.0, 23.0)];
//    [setBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"person_icon5@2x"] forState:UIControlStateNormal];
//    [setBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"person_icon5@2x"] forState:UIControlStateHighlighted];
//    [setBtn addTarget:self action:@selector(setBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [headerView addSubview:setBtn];

}
-(void)initFooterView{
        //footer
        CGRect rect = [UIScreen mainScreen].bounds;
        NSLog(@"rect==========%@",NSStringFromCGRect(rect));
        UIImage *footerBg = [[ApplicationUtils getLocalImage:@"Navigation-Bar_backgroud_44@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
        UIImageView *footer = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,rect.size.width, 64)];
        [footer setImage:footerBg];
       int start = (rect.size.width-20.0*5)/6;
        NSInteger offset = 20.0;
        //返回按钮
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:CGRectMake(start+20*0, 10+offset, 30.0, 30.0)];
        [backBtn setImage:[ApplicationUtils getLocalImage:@"NavigationCloseButton@2x"] forState:UIControlStateNormal];
        [backBtn setImage:[ApplicationUtils getLocalImage:@"NavigationCloseButtonPressed@2x"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:backBtn];
    
        //清除
        UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [upButton setImage:[ApplicationUtils getLocalImage:@"NavigationDeleteButton@2x"] forState:UIControlStateNormal];
        [upButton setImage:[ApplicationUtils getLocalImage:@"NavigationDeleteButtonDisabled@2x"] forState:UIControlStateSelected];
        [upButton setImage:[ApplicationUtils getLocalImage:@"NavigationDeleteButtonPressed@2x"] forState:UIControlStateHighlighted];
        [[upButton titleLabel] setFont:[UIFont systemFontOfSize:14.0]];
        [upButton setFrame:CGRectMake(start*2+20, 10+offset, 30.0, 30.0)];
        [upButton addTarget:self action:@selector(clearBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:upButton];
    
        //截屏
        UIButton *answerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [answerButton setTitle:@"保存" forState:UIControlStateNormal];
        [answerButton setTitle:@"保存" forState:UIControlStateHighlighted];
        [answerButton.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
//        [answerButton setImage:[ApplicationUtils getLocalImage:@"NavigationCollectButtonPressed@2x"] forState:UIControlStateHighlighted];
//        [answerButton setImage:[ApplicationUtils getLocalImage:@"NavigationCollectButton@2x"] forState:UIControlStateNormal];
//        [answerButton setImage:[ApplicationUtils getLocalImage:@"NavigationCollectButtonPressed@2x"] forState:UIControlStateHighlighted];
        [answerButton setTitleColor:[ApplicationUtils stringTOColor:@"#1cb6a7"] forState:UIControlStateHighlighted];
        [answerButton setFrame:CGRectMake(start*3+20.0*2, 10+offset, 30.0, 30.0)];
        [answerButton addTarget:self action:@selector(cutBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:answerButton];
        //下一步
        UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [favButton setImage:[ApplicationUtils getLocalImage:@"NavigationScratchRedoButton@2x"] forState:UIControlStateNormal];
        [favButton setImage:[ApplicationUtils getLocalImage:@"NavigationScratchRedoButtonDisabled"] forState:UIControlStateHighlighted];
//        [favButton setTitle:@"下一步" forState:UIControlStateNormal];
//        [favButton setTitle:@"下一步" forState:UIControlStateHighlighted];
        [[favButton titleLabel] setFont:[UIFont systemFontOfSize:14.0]];
//        [favButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
//        [favButton setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 30)];
        [favButton setFrame:CGRectMake(start*4+20.0*3, 10+offset, 30.0, 30.0)];
        [favButton addTarget:self action:@selector(undoBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [footer addSubview:favButton];
        //撤销
        UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [downButton setImage:[ApplicationUtils getLocalImage:@"NavigationScratchUndoButtonPressed@2x"] forState:UIControlStateNormal];
        [downButton setImage:[ApplicationUtils getLocalImage:@"NavigationScratchUndoButtonPressed@2x"] forState:UIControlStateHighlighted];
        [downButton setImage:[ApplicationUtils getLocalImage:@"NavigationScratchUndoButtonDisabled@2x"] forState:UIControlStateSelected];
        [downButton setFrame:CGRectMake(start*5+20.0*4, 10+offset, 30.0, 30.0)];
        [[downButton titleLabel] setFont:[UIFont systemFontOfSize:14.0]];
        [downButton addTarget:self action:@selector(doBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [footer addSubview:downButton];
        [footer setUserInteractionEnabled:YES];
        [self addSubview:footer];
}
-(void)doBtnClicked:(UIButton *)sender{
    [self.drawView revocation];
}
-(void)undoBtnClicked:(UIButton *)sender{
    [self.drawView refrom];
}
-(void)cutBtnClicked:(UIButton *)sender{
    [self back];//保存返回
    UIGraphicsBeginImageContext(bgView.bounds.size);
    [_drawView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
//    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    if (self.draftBlock) {
        self.draftBlock(self,image);
    }
//    //截屏成功
//    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"保存成功" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//    [alertView show];
}
-(void)clearBtnClicked:(UIButton *)sender{
    [self.drawView clear];
}
-(id)initWithFrame:(CGRect)frame WithView:(UIView *)aView{
    self = [super initWithFrame:frame];
    if (self) {
        
//        [self initNavBar];
//        self.backgroundColor = [UIColor clearColor];
        CGRect rect = [UIScreen mainScreen].bounds;
        bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, rect.size.width, rect.size.height-64)];
        UIImage *bgImage = [[UIImage imageNamed:@"card_btn_hover.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        bgImage = [ApplicationUtils imageWithGaussianBlur9:bgImage];
        [bgView setImage:bgImage];
        [bgView setUserInteractionEnabled:YES];
        self.drawView=[[MyView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height-64)];
        [self.drawView setBackgroundColor:[UIColor clearColor]];
        [self.drawView setAlpha:1.0];
        [bgView addSubview: self.drawView];
        [self addSubview:bgView];
        //    [self.view sendSubviewToBack:self.drawView];
        [aView addSubview:self];
        [self initFooterView];
    }
    return self;
}
-(void)pushToView{
    CGRect rect = [UIScreen mainScreen].bounds;
    self.frame = CGRectMake(0, rect.size.height, self.frame.size.width, self.frame.size.height);
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(rect.origin.x, rect.origin.y, self.frame.size.width, self.frame.size.height);
    }];
}
@end
